/************************************************************************/
// Copyright (C) 2016, han_gangbiao. All rights reserved.
// Use of this source code is governed by a GPL-v2 license that can be found in the License file.
// 
// [Time]:      2016-1-4 21:53:17
// [Author]:    han_gangbiao [http://my.oschina.net/jackhen]
// [Info]:      
/************************************************************************/

#include "NRSocket.h"

NRSocket::NRSocket()
{
	memset(m_ip, 0, sizeof(m_ip));
	m_port = 0;
	m_buffer_send_Q.clear();
	m_buffer_recv_Q.clear();
}

NRSocket::~NRSocket()
{
	clear_send_buffer();
	clear_recv_buffer();
	close();
}

void NRSocket::reset()
{
	memset(m_ip, 0, sizeof(m_ip));
	m_port = 0;
	clear_send_buffer();
	clear_recv_buffer();
}

int NRSocket::send_buffer_size()
{
	smart::guard_t guard(&m_mtx_buffer_send);
	return (int)m_buffer_send_Q.size();
}

int NRSocket::push_row_to_send_buffer_back( net_buffer_row_t* row )
{
	smart::guard_t guard(&m_mtx_buffer_send);
	m_buffer_send_Q.push_back(row);
	return 0;
}

int NRSocket::push_row_to_send_buffer_front( net_buffer_row_t* row )
{
	smart::guard_t guard(&m_mtx_buffer_send);
	m_buffer_send_Q.push_front(row);
	return 0;
}

smart::ref_ptr<net_buffer_row_t> NRSocket::pop_row_from_send_buffer_back()
{
	smart::ref_ptr<net_buffer_row_t> row = NULL;
	smart::guard_t guard(&m_mtx_buffer_send);
	row = m_buffer_send_Q.back();
	m_buffer_send_Q.pop_back();
	return row;
}

smart::ref_ptr<net_buffer_row_t> NRSocket::pop_row_from_send_buffer_front()
{
	smart::ref_ptr<net_buffer_row_t> row = NULL;
	smart::guard_t guard(&m_mtx_buffer_send);
	row = m_buffer_send_Q.front();
	m_buffer_send_Q.pop_front();
	return row;
}

int NRSocket::recv_buffer_size()
{
	smart::guard_t guard(&m_mtx_buffer_recv);
	return (int)m_buffer_recv_Q.size();
}

int NRSocket::push_row_to_recv_buffer_back( net_buffer_row_t* row )
{
	smart::guard_t guard(&m_mtx_buffer_recv);
	m_buffer_recv_Q.push_back(row);
	return 0;
}

int NRSocket::push_row_to_recv_buffer_front( net_buffer_row_t* row )
{
	smart::guard_t guard(&m_mtx_buffer_recv);
	m_buffer_recv_Q.push_front(row);
	return 0;
}

smart::ref_ptr<net_buffer_row_t> NRSocket::pop_row_from_recv_buffer_back()
{
	smart::ref_ptr<net_buffer_row_t> row = NULL;
	smart::guard_t guard(&m_mtx_buffer_recv);
	row = m_buffer_recv_Q.back();
	m_buffer_recv_Q.pop_back();
	return row;
}

smart::ref_ptr<net_buffer_row_t> NRSocket::pop_row_from_recv_buffer_front()
{
	smart::ref_ptr<net_buffer_row_t> row = NULL;
	smart::guard_t guard(&m_mtx_buffer_recv);
	row = m_buffer_recv_Q.front();
	m_buffer_recv_Q.pop_front();
	return row;
}

void NRSocket::clear_send_buffer()
{
	smart::guard_t guard(&m_mtx_buffer_send);
	NetBufferRowQueue::iterator it_send = m_buffer_send_Q.begin();
	for(; it_send!=m_buffer_send_Q.end(); ++it_send)
	{
		net_buffer_pool_t::instance()->recycle(*it_send);
	}
	m_buffer_send_Q.clear();
}

void NRSocket::clear_recv_buffer()
{
	smart::guard_t guard(&m_mtx_buffer_recv);
	NetBufferRowQueue::iterator it_recv = m_buffer_recv_Q.begin();
	for(; it_recv!=m_buffer_recv_Q.end(); ++it_recv)
	{
		net_buffer_pool_t::instance()->recycle(*it_recv);
	}
	m_buffer_recv_Q.clear();
}

void NRSocket::lock_send()
{
    m_send_lock.lock();
}
void NRSocket::unlock_send()
{
    m_send_lock.unlock();
}

void NRSocket::lock_recv()
{
    m_recv_lock.lock();
}
void NRSocket::unlock_recv()
{
    m_recv_lock.unlock();
}


